#include "adminwidget.h"
#include "ui_adminwidget.h"
#include "databasemanager.h"
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include "addeventdialog.h"

adminwidget::adminwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::adminwidget),
    eventModel(new QSqlQueryModel(this)),
    reservationModel(new QSqlQueryModel(this))
{
    ui->setupUi(this);
    loadEvents();
    loadReservations();
}

adminwidget::~adminwidget()
{
    delete ui;
}

void adminwidget::loadEvents()
{
    eventModel->setQuery("SELECT event_id, name, type, start_date, end_date FROM Exhibitions_Events",
                         DatabaseManager::instance().getDatabase());
    ui->tableViewEvents->setModel(eventModel);
    ui->tableViewEvents->resizeColumnsToContents();
}

void adminwidget::loadReservations()
{
    reservationModel->setQuery(R"(
        SELECT visit_id, visitor_id, venue_id, visit_date, visit_time, event_id
        FROM Venue_Visits
        WHERE satisfaction_rating IS NULL
    )", DatabaseManager::instance().getDatabase());
    ui->tableViewReservations->setModel(reservationModel);
    ui->tableViewReservations->resizeColumnsToContents();
}

void adminwidget::on_addEventButton_clicked()
{
    addeventdialog dialog(this);
       if (dialog.exec() == QDialog::Accepted) {
           loadEvents();  // 刷新活动列表
       }
}

void adminwidget::on_deleteEventButton_clicked()
{
    QModelIndex index = ui->tableViewEvents->currentIndex();
    if (!index.isValid()) return;

    int eventId = index.sibling(index.row(), 0).data().toInt();
    QSqlQuery query(DatabaseManager::instance().getDatabase());
    query.prepare("DELETE FROM Exhibitions_Events WHERE event_id = :id");
    query.bindValue(":id", eventId);

    if (!query.exec()) {
        QMessageBox::warning(this, "删除失败", query.lastError().text());
    } else {
        QMessageBox::information(this, "成功", "活动已删除！");
        loadEvents();
    }
}

void adminwidget::on_approveButton_clicked()
{
    QModelIndex index = ui->tableViewReservations->currentIndex();
    if (!index.isValid()) return;

    int visitId = index.sibling(index.row(), 0).data().toInt();
    QSqlQuery query(DatabaseManager::instance().getDatabase());
    query.prepare("UPDATE Venue_Visits SET satisfaction_rating = 0 WHERE visit_id = :id");
    query.bindValue(":id", visitId);

    if (!query.exec()) {
        QMessageBox::warning(this, "审批失败", query.lastError().text());
    } else {
        QMessageBox::information(this, "成功", "预约已同意！");
        loadReservations();
    }
}

void adminwidget::on_rejectButton_clicked()
{
    QModelIndex index = ui->tableViewReservations->currentIndex();
    if (!index.isValid()) return;

    int visitId = index.sibling(index.row(), 0).data().toInt();
    QSqlQuery query(DatabaseManager::instance().getDatabase());
    query.prepare("DELETE FROM Venue_Visits WHERE visit_id = :id");
    query.bindValue(":id", visitId);

    if (!query.exec()) {
        QMessageBox::warning(this, "拒绝失败", query.lastError().text());
    } else {
        QMessageBox::information(this, "成功", "预约已拒绝！");
        loadReservations();
    }
}
